package com.cash.lemoloan.aflib;

import android.content.Context;
import android.text.TextUtils;

import com.appsflyer.AppsFlyerConversionListener;
import com.appsflyer.AppsFlyerLib;

import java.util.Map;

import com.cash.lemoloan.statisticlib.Utils.AppInfoUtils;
import com.cash.lemoloan.statisticlib.Utils.PreferencesUtil;


public class AppsFlyerUtils {
    private Context context;
    private final String organic = "Organic";
    private final String noOrganic = "Non-organic";
    public AppsFlyerUtils(Context context, String afKey) {
        this.context = context;
        AppsFlyerLib.getInstance().init(afKey, conversionDataListener,context);
        AppsFlyerLib.getInstance().start(AfApplication.getInstance());
        AppsFlyerLib.getInstance().setDebugLog(false);
    }

    private AppsFlyerConversionListener conversionDataListener = new AppsFlyerConversionListener() {
        @Override
        public void onConversionDataSuccess(Map<String, Object> map) {
            String af_status = "";
            String adChannel = AppInfoUtils.getAppDefaultChannel();
            String gpChannel = "1000000001";
            String otherChannel = "1000000099";
            if (map == null){
                adChannel = otherChannel;
            }else{
                String afStatus = map.get("af_status") == null?"":map.get("af_status").toString();
                String media_source = map.get("media_source") == null?"":map.get("media_source").toString();
                String isFirstLaunch = map.get("is_first_launch") == null?"":map.get("is_first_launch").toString();
                PreferencesUtil.putString("mediaSource",media_source);
                if(organic.equals(afStatus)){
                    af_status = organic;
                    adChannel = gpChannel;
                }else if(noOrganic.equals(afStatus)){
                    try {
                        af_status = noOrganic;
                        adChannel = otherChannel;
                        String campaign = map.get("campaign") == null?"":map.get("campaign").toString();
                        if(!TextUtils.isEmpty(campaign)){

                            if(campaign.contains("LemonLoan")){
                                int last = campaign.indexOf("LemonLoan");
                                String substringLast = campaign.substring(last);
                                if(last>0 && !TextUtils.isEmpty(substringLast)){
                                    int index = substringLast.indexOf("_");
                                    String ch = substringLast.substring(index + 1);
                                    if(index > 0 && !TextUtils.isEmpty(ch) && ch.length() == 10){
                                        adChannel = ch;
                                        Integer.parseInt(ch);
                                    }
                                }
                            }else{
                                int i = campaign.indexOf("_");
                                String substring = campaign.substring(0, i);
                                if(i > 0 && !TextUtils.isEmpty(substring) && substring.length() == 10){
                                    adChannel = substring;
                                    Integer.parseInt(substring);
                                }
                            }


                            if(!otherChannel.substring(0,3).equals(adChannel.substring(0,3))){
                                adChannel = otherChannel;
                            }
                        }
                    }catch (Exception e){
                        adChannel = otherChannel;
                    }
                }
                AppInfoUtils.initAppADChannel(adChannel);
            }


        }

        @Override
        public void onConversionDataFail(String s) {
        }




        @Override
        public void onAppOpenAttribution(Map<String, String> map) {

        }

        @Override
        public void onAttributionFailure(String s) {
        }
    };

    public void onEvent(String eventType, Map data) {
        AppsFlyerLib.getInstance().logEvent(context, eventType , data);
    }

    public static String getAfUid(){
        return AppsFlyerLib.getInstance().getAppsFlyerUID(AfApplication.getInstance());
    }

}
